'use strict';
//线路方案调度
App.controller('dispatchController', ['$rootScope', '$scope', 'Page', function($rootScope, $scope, Page) {

	 $scope.driverMap = new Map();
	 //获取全部司机信息
	 Page.ajaxGet('driver/query.do', null, function(resp) {
	        $scope.driverList = resp.data;
	        for(var i=0;i<$scope.driverList.length;i++){
	        	var driver = $scope.driverList[i];
	        	$scope.driverMap.put(driver.driverNo,driver);
	        };
	    });
	 //获取全部车辆信息
	 Page.ajaxGet('vehicle/getAllVehicle.do', null, function(resp) {
		 $scope.vehicleList = resp.data;
	 });
	
	 $scope.dispatchGrid = {
		 column_defs: [
	        {
	            target: 4,
	            buttons: [
	                {
	                    name: '司机分配',
	                    class: 'btn purple',
	                    icon: 'fa-cog',
	                    event: 'allocationDriver'
	                }
	            ]
	        }
    	],
    	allocationDriver: function(row) { //分配司机
    		
            $scope.allocationModal.show();
            $scope.linePlan = row;
		},
	
	saveDriver : function(){
		var param = {};
		param.drivers = [];
		param.vehicles = [];
		$('input[name="drivers"]:checked').each(function(){    
			 var driver = {};
			 var driverNo =  $(this).val();
			 param.drivers.push($scope.driverMap.get(driverNo));
		 });
		$('input[name="vehicles"]:checked').each(function(){    
			 var vehicle = {};
			 vehicle.vehicleNo =  $(this).val();
			 param.vehicles.push(vehicle);
		 });  
		if( param.vehicles.length != param.drivers.length){
			Page.notice("司机与车辆数量不一致 !");
			return false;
		}
		 param.planId =  $scope.linePlan.planId;
		 param.lineId =  $scope.linePlan.lineId;
		 param.lineName =  $scope.linePlan.lineName;
		 param.departDate =  $scope.departDate;
		 Page.ajaxPost('dispatch/add.do', param, function(resp) {
			  $scope.allocationModal.hide();
		    });
		},
	changeDepartDate : function(){
		var param = {};
		param.departDate = $scope.departDate;
		param.planId = $scope.linePlan.planId;
		Page.ajaxGet('dispatch/getByPlanIdAndDepartDate.do', param, function(resp) {
	        $scope.planDrivers = resp.data;
	        // 全部司机信息
	        for(var m=0;m<$scope.driverList.length;m++){
       		 $scope.driverList[m].hasChoice = false;
	        };
	        // 全部车辆信息
	        for(var n=0;n<$scope.vehicleList.length;n++){
	       		 $scope.vehicleList[n].hasChoice = false;
	        };
	        for(var i=0;i<$scope.planDrivers.length;i++){
	        	var planDriver = $scope.planDrivers[i];
	        	var driverNo = planDriver.driverNo;// 司机编号
	        	var vehicleNo = planDriver.vehicleNo;//  车牌信息
	        	
	    	    for(var j=0;j<$scope.driverList.length;j++){
	        		 if($scope.driverList[j].driverNo == driverNo ){// 该司机已分配
	        			 $scope.driverList[j].hasChoice = true;
	        			 break;
	        		 }
	 	         };
	 	         
	 	        for(var l=0;l<$scope.vehicleList.length;l++){
	        		 if($scope.vehicleList[l].vehicleNo == vehicleNo ){// 该车辆已分配
	        			 $scope.vehicleList[l].hasChoice = true;
	        			 break;
	        		 }
	 	         };
	        };
	    });
	}	
	};
}]);
